package com.qf.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsAdmin;
import com.qf.pojo.DtsBrand;
import com.qf.pojo.DtsCategory;
import com.qf.pojo.DtsUser;
import com.qf.service.DtsAdminService;
import com.qf.service.DtsBrandService;
import com.qf.service.DtsCategoryService;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import com.qf.vo.DtsBrandVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/admin/brand")
public class DtsAdminBrandController {

    @Autowired
    DtsAdminService adminService;
    @Autowired
    DtsCategoryService categoryService;
    @Autowired
    DtsBrandService brandService;

    @RequestMapping("/list")
    public Object list(
            @RequestParam(name = "page", required = false, defaultValue = "1") Integer page,
            @RequestParam(name = "limit", required = false, defaultValue = "10") Integer limit,
            String username, String id, String sort, String order
    ) {
        IPage<DtsBrand> ipage = brandService.queryPage(page, limit, sort, order, username, id);
        List<DtsBrand> records = ipage.getRecords();
        List<DtsBrandVo> brandVos=new ArrayList<>();
        for (DtsBrand record : records) {
            Integer id1 = record.getDefaultCategoryId();
            DtsCategory category = categoryService.findOne(id1);
            Integer[] i={category.getPid(),id1};
            DtsBrandVo brandVo=new DtsBrandVo();
            org.springframework.beans.BeanUtils.copyProperties(record,brandVo);
            brandVo.setCategoryIds(i);
            brandVos.add(brandVo);
        }
        Map<String, Object> data = new HashMap<>();
        data.put("total", ipage.getTotal());
        data.put("items", brandVos);
        return ResponseUtil.ok(data);
    }

    @RequestMapping("/catAndAdmin")
    public Object catAndAdmin() {
        Map<String, Object> data = new HashMap<>();
        List<CatVo> catVos = categoryService.findList();
        List<DtsAdmin> admins = adminService.find();
        List<Map<String, Object>> adminList = admins.stream().map(admin -> {
            Map<String, Object> admin1 = new HashMap<>();
            admin1.put("value", admin.getId());
            admin1.put("label", admin.getUsername());
            return admin1;
        }).collect(Collectors.toList());
        System.out.println(catVos);
//        for (DtsAdmin admin : admins) {
//            Map<String,Object> admin1=new HashMap<>();
//            admin1.put("value",admin.getId());
//            admin1.put("level",admin.getUsername());
//            adminList.add(admin1);
//        }
        data.put("adminList", adminList);
        data.put("categoryList", catVos);
        return ResponseUtil.ok(data);
    }



    @RequestMapping("/create")
    public Object create(@RequestBody DtsBrand brand) {

//        System.out.println(brand);
        DtsBrand data = brandService.create(brand);
        return ResponseUtil.ok(data);

    }

    @RequestMapping("/update")
    public Object update(@RequestBody DtsBrand brand) {
        DtsBrand data = brandService.update(brand);
        return ResponseUtil.ok(data);

    }
    @RequestMapping("/delete")
    public Object delete(@RequestBody DtsBrand brand){
        brandService.delete(brand);
        return ResponseUtil.ok();
    }
}